
package com.kanjian.music.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

/**
 * 音乐栏下的子Fragment父类 控制导航及Banner
 * 
 * @author xuyanjun@kanjian.com
 */
public class MusicSubBaseFragment extends BaseFragment {

    protected ListView mListView;

    protected MusicFragment mMusicFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mMusicFragment = (MusicFragment) getParentFragment();
        return null;
    }

    protected void setListScrollListener() {
        mListView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (visibleItemCount > 0) {
                    if (firstVisibleItem == 0) {
                        View curV = view.getChildAt(0);
                        mMusicFragment.changeIndicatorHeight(curV.getBottom());
                        mMusicFragment.syncOtherFragmentsListScroll(firstVisibleItem, curV.getTop());
                    } else {
                        mMusicFragment.changeIndicatorHeight(0);
                    }
                }
            }
        });
    }

    public void smoothScrollToTop(int height, int minHeight) {
        if (mListView.getFirstVisiblePosition() == 0) {
            View firstView = mListView.getChildAt(0);
            if (firstView != null && firstView.getBottom() > minHeight) {
                mListView.smoothScrollBy(height + firstView.getTop(), 500);
            }
        }
    }

    /**
     * 使MuiscFragment下的每个Fragment中的ListView头部滚动同步
     */
    public void syncListViewScroll(int firstItem, int y) {
        mListView.setSelectionFromTop(firstItem, y);
    }

}
